﻿
using System.Text.RegularExpressions;

namespace Test30.Engine
{
    public class Noun : INoun
    {
        #region INoun Members

        public string NounText => _singular;
        public Person Person => Person.Third;
        public string Pronoun => "it";
        public string Possessive => "its";

        #endregion

        public string Singular => _singular;
        public string Plural => _plural;

        public Noun(string name)
        {
            //### bob: make it support "sta[ff|ves] of fire balls" syntax like Sentence.Format allows for verbs

            // see if the string is formatted like "foo|foos" or "foo[s]"
            if (name.Contains("|"))
            {
                string[] parts = name.Split('|');

                _singular = parts[0];
                _plural = parts[1];
            }
            else
            {
                // parse out verbs formatted like "hit[s]" where "hit" is
                // first and second person and "hits" is third.
                Match match = SRegex.Match(name);

                string before = match.Groups["before"].Value;
                _singular = before;
                _plural = before;

                for (int i = 0; i < match.Groups["optional"].Captures.Count; i++)
                {
                    _plural += match.Groups["optional"].Captures[i].Value;

                    _singular += match.Groups["after"].Captures[i].Value;
                    _plural += match.Groups["after"].Captures[i].Value;
                }
            }
        }

        private static readonly Regex SRegex = new Regex(@"(?<before>[^\[]*)(\[(?<optional>[^\]]*)\](?<after>[^\[]*))*", RegexOptions.Compiled);

        private readonly string _singular;
        private readonly string _plural;
    }
    public static class Verbs
    {
        public const string Hit = "hit[s]";
    }
}
